<?php
header('Content-Type: application/json');
Header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Origin:*');   //跨域

require './mysql_init.php';

//判断数据库中是否有此session_id
function is_have_session_id($pdo,$session_id){
    try {
        // 准备查询语句
        $query = "SELECT count(*) FROM session WHERE session_id = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $session_id);
        
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['count(*)'];      //把查询结构赋值给$result
        // 根据结果判断是否存在
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//利用session_id获取邮箱
function get_email($pdo,$session_id){
    try {
        // 准备查询语句
        $query = "SELECT email FROM session WHERE session_id = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $session_id);
        
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0]['email'];      //把查询结构赋值给$result
        // 根据结果判断是否存在
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
//利用邮箱找到对应的用户信息
function get_news($pdo,$email){
    try {
        // 准备查询语句
        $query = "SELECT name,is_seller FROM users WHERE email = ?";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $email);
        
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll()[0];      //把查询结构赋值给$result
        // 根据结果判断是否存在
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}
if($_SERVER['REQUEST_METHOD'] === 'POST'){    //判断是否是post请求
    $json=file_get_contents('php://input');  //获取post请求中的原始数据
    $post_json=json_decode($json,true);       //把原始数据转化成json
    $response = array(
        'status' => 0,
        'message' => '是post请求',
        'seeeion_id'=>'',
        'usernmae'=>'',
        'is_seller'=>0
        );
    $session_id=$post_json['session_id'];      //获取session_id
    if(is_have_session_id($pdo,$session_id)){
        $email=get_email($pdo,$session_id);
        $news=get_news($pdo,$email);
        $response = array(
            'status' => 1,
            'message' => 'ok',
            'seeeion_id'=>'',
            'usernmae'=>$news['name'],
            'is_seller'=>$news['is_seller']
            );
    }else{
        $response = array(
            'status' => 0,
            'message' => '凭证无效',
            'seeeion_id'=>'',
            'usernmae'=>'',
            'is_seller'=>0
            );
    }
    echo json_encode($response);
}else{         //不是post请求
    $response = array(
        'status' => 0,
        'message' => '不是post请求',
        'seeeion_id'=>'',
        'usernmae'=>'',
        'is_seller'=>0
        );
    echo json_encode($response);
}
// $auth=$_SERVER['HTTP_AUTHORIZATTON'];






?>